package com.feihong.txgw.adapter.searchGoods;

import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.adapter.base.BaseMultiItemQuickAdapter;
import com.adapter.base.BaseViewHolder;
import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.model.bean.searchList.SearchListEntity;
import com.feihong.txgw.model.bean.searchList.SearchPhoneListModel;
import com.feihong.txgw.utils.glidehttps.GlideOptionsSetting;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName SearchStoreAndGoodsAdapter
 * @package com.feihong.txgw.adapter.searchGoods
 * @data 2018/7/20 11:22
 * @Describe  门店、商品搜索 列表adapter
 * @Change
 */
public class SearchStoreAndGoodsAdapter extends BaseMultiItemQuickAdapter<SearchListEntity, BaseViewHolder> {

    private TextView mKeyword0,mKeyword1,mKeyword2,mKeyword3;
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public SearchStoreAndGoodsAdapter(List<SearchListEntity> data) {
        super(data);
        addItemType(SearchListEntity.SEARCH_GOODS_TYPE,R.layout.item_search_lv);
        addItemType(SearchListEntity.SEARCH_STORE_TYPE,R.layout.item_search_moren_lv);
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchListEntity item) {
        switch (helper.getItemViewType()) {
            case SearchListEntity.SEARCH_GOODS_TYPE:
                SearchPhoneListModel.DataBean.GoodsListBean goodsListBean = (SearchPhoneListModel.DataBean.GoodsListBean) item;
                helper.setText(R.id.search_store_id,goodsListBean.getSku_name());
                helper.setText(R.id.shop_price,"¥" + goodsListBean.getShop_price());
                List<String> keyword = goodsListBean.getKeyword();
                int i = keyword.size();
                mKeyword0 = helper.getView(R.id.keyword0);
                mKeyword1 = helper.getView(R.id.keyword1);
                mKeyword2 = helper.getView(R.id.keyword2);
                mKeyword3 = helper.getView(R.id.keyword3);
                if (i == 1) {
                    mKeyword0.setVisibility(View.VISIBLE);
                    mKeyword0.setText(keyword.get(i - 1));

                    mKeyword1.setVisibility(View.GONE);
                    mKeyword2.setVisibility(View.GONE);
                    mKeyword3.setVisibility(View.GONE);

                } else if (i == 2) {
                    mKeyword0.setVisibility(View.VISIBLE);
                    mKeyword0.setText(keyword.get(i - 1));


                    mKeyword1.setVisibility(View.VISIBLE);
                    mKeyword1.setText(keyword.get(i - 2));

                    mKeyword2.setVisibility(View.GONE);
                    mKeyword3.setVisibility(View.GONE);
                } else if (i == 3) {
                    mKeyword0.setVisibility(View.VISIBLE);
                    mKeyword0.setText(keyword.get(i - 1));
                    mKeyword1.setVisibility(View.VISIBLE);
                    mKeyword1.setText(keyword.get(i - 2));
                    mKeyword2.setVisibility(View.VISIBLE);
                    mKeyword2.setText(keyword.get(i - 3));

                    mKeyword3.setVisibility(View.GONE);

                } else if (i == 4) {
                    mKeyword0.setVisibility(View.VISIBLE);
                    mKeyword0.setText(keyword.get(i - 1));
                    mKeyword1.setVisibility(View.VISIBLE);
                    mKeyword1.setText(keyword.get(i - 2));
                    mKeyword2.setVisibility(View.VISIBLE);
                    mKeyword2.setText(keyword.get(i - 3));
                    mKeyword3.setVisibility(View.VISIBLE);
                    mKeyword3.setText(keyword.get(i - 4));
                }

                List<SearchPhoneListModel.DataBean.GoodsListBean.SpecBean> spec = goodsListBean.getSpec();
                StringBuffer sb = new StringBuffer();
                for (int e = 0; e < spec.size(); e++) {
                    sb.append(spec.get(e).getValue() + "   ");
                }
                String str = sb.toString();
                helper.setText(R.id.item_store_name,str);
                ImageView imageView = helper.getView(R.id.iv_pic);
                Glide.with(mContext).load(goodsListBean.getThumb()).into(imageView);

                break;
            case SearchListEntity.SEARCH_STORE_TYPE:
                SearchPhoneListModel.DataBean.StoreListBean storeListBean = (SearchPhoneListModel.DataBean.StoreListBean) item;
                helper.setText(R.id.distance,storeListBean.getDistance() + "Km");
                helper.setText(R.id.search_more_store_name,storeListBean.getStore_name());
                List<String> Store_image = storeListBean.getStore_image();
                CircleImageView imageView1 = helper.getView(R.id.store_image);
                if (Store_image.size() > 0) {
                    String s = Store_image.get(0);
                    byte[] bytes = Base64.decode(s.getBytes(), Base64.DEFAULT);
                    String res = new String(bytes);
                    Glide.with(mContext).load(res).apply(GlideOptionsSetting.getDefaultOptions(R.mipmap.user_head,R.mipmap.user_head)).into(imageView1);
                }
                break;
            default:
                break;
        }
    }
}
